package org.example.day;

import java.util.ArrayList;
import java.util.List;

/**
 * @Description: TODO
 * @Author wyatt
 * @Data 2024/09/08 21:08
 */
public class Solution77 {
    public int[] sortedSquares(int[] nums) {
        List<Integer> negList = new ArrayList<>();
        int[] res = new int[nums.length];

        int start = 0;
        for (int i = 0; i < nums.length; i++) {
            if(nums[i] < 0){
                negList.add(nums[i]);
            }else {

                while (negList.size() > 0 && (nums[i] + negList.get(negList.size()-1) >= 0)){
                    res[start] = negList.get(negList.size()-1) * negList.get(negList.size()-1);
                    negList.remove(negList.size()-1);
                    start++;
                }

                //判断 i VS list最后一个
                if(negList.size() == 0){
                    res[start] = nums[i] * nums[i];
                    start++;
                    continue;
                }

                if(nums[i] + negList.get(negList.size()-1) > 0){
                    res[start] = nums[i] * nums[i];
                    start++;
                }
            }
        }

        while (negList.size() > 0 ){
            res[start] = negList.get(negList.size()-1) * negList.get(negList.size()-1);
            negList.remove(negList.size()-1);
            start++;
        }

        return res;
    }
}
